﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System;
using UnityEngine.UI;

/// <summary>
/// 旅客安检管理
/// </summary>
public class PassengerManager : MonoBehaviour
{
    public static PassengerManager Instance;
    [Header("检查"), SerializeField]
    private bool isScan;
    [Header("检查类型")]
    public PassengerType scanType;
    public ScanTypeInfo scanInfo;
    public int scanRightNum;
    [Header("备忘录")]
    public DOTweenAnimation calepinBtn;
    public GameObject caleMask;
    public GameObject calepin;
    [Header("旅客")]
    public List<GameObject> passengerPerfabs;
    public Transform passengerParent;
    public List<Transform> passengerPos;
    public Transform passengerExitPos;
    public List<GameObject> currentPassenger;
    [Header("平板扫描仪")]
    public GameObject scanTab;
    public GameObject xrayModel;
    [NonSerialized]
    public Animator xrayModelAnim;
    private Vector3 scanTabPos;

    // 位置信息
    private Vector3 calepinBtnPos_0 = new Vector3(200, 250f, 0);
    private Vector3 calepinBtnPos_1 = new Vector3(0, 250f, 0);
    private Vector3 calepinPos_0 = new Vector3(0.528f, 1.027f, 0);
    private Vector3 calepinPos_1 = new Vector3(0.528f, 0.5f, 0);


    private void Awake()
    {
        Instance = this;
    }

    private void Start()
    {
        scanTabPos = scanTab.transform.position;
        xrayModelAnim = xrayModel.GetComponentInChildren<Animator>();
    }

    // 旅客初始化
    public void PassengerInit()
    {
        isScan = false;
        scanRightNum = 0;
        SetCurrentScanType();
        RandomPassenger();
        StopScanPassenger();
        Invoke("ShowCalepin", 0.5f);
    }

    // 随机旅客
    private void RandomPassenger()
    {
        List<GameObject> temp = new List<GameObject>(passengerPerfabs);
        GameObject obj = null;
        for (int i = 1; i < 4; i++)
        {
            int randomIndex = UnityEngine.Random.Range(0, temp.Count);
            obj = Instantiate(temp[randomIndex], passengerParent);
            obj.transform.position = passengerPos[i].position;
            obj.GetComponent<PassengerAnim>().currentIndex = i;
            obj.GetComponent<PassengerAnim>().passengerType = GetRandomType();
            currentPassenger.Add(obj);
            temp.RemoveAt(randomIndex);
        }
    }

    // 设置当前检查类型
    private void SetCurrentScanType()
    {
        scanType = GetScanType();
        scanInfo.dateText.text = "第 " + PlayerPrefs.GetInt("Date", 1) + " 天";
        if (UnityEngine.Random.Range(0, 2) == 0)
            scanInfo.timeText.text = "上午 " + UnityEngine.Random.Range(1, 13) + ":00";
        else
            scanInfo.timeText.text = "下午 " + UnityEngine.Random.Range(1, 13) + ":00";
        scanInfo.scanTitles.text = scanInfo.typeText.text = GetScanInfo(scanType);
        scanInfo.typeIcon.sprite = GetScanTypeIcon(scanType);
    }

    // 获取随机类型
    private PassengerType GetRandomType()
    {
        switch (UnityEngine.Random.Range(0, 7))
        {
            case 0:
                return PassengerType.Normal;
            case 1:
                return PassengerType.Weapons;
            case 2:
                return PassengerType.Tools;
            case 3:
                return PassengerType.Electronic;
            case 4:
                return PassengerType.Foods;
            case 5:
                return PassengerType.Bottles;
            default:
                return PassengerType.Normal;
        }
    }

    // 获取检查类型
    private PassengerType GetScanType()
    {
        switch (UnityEngine.Random.Range(1, 6))
        {
            case 1:
                return PassengerType.Weapons;
            case 2:
                return PassengerType.Tools;
            case 3:
                return PassengerType.Electronic;
            case 4:
                return PassengerType.Foods;
            case 5:
                return PassengerType.Bottles;
            default:
                return PassengerType.Normal;
        }
    }

    private String GetScanInfo(PassengerType type)
    {
        switch (type)
        {
            case PassengerType.Weapons:
                return "禁止携带武器";
            case PassengerType.Tools:
                return "禁止携带工具";
            case PassengerType.Electronic:
                return "禁止携带电子设备";
            case PassengerType.Foods:
                return "禁止携带食物";
            case PassengerType.Bottles:
                return "禁止携带玻璃瓶";
            default:
                return "没有禁止事项";
        }
    }

    private Sprite GetScanTypeIcon(PassengerType type)
    {
        switch (type)
        {
            case PassengerType.Weapons:
                return scanInfo.typeIcons[0];
            case PassengerType.Tools:
                return scanInfo.typeIcons[1];
            case PassengerType.Electronic:
                return scanInfo.typeIcons[2];
            case PassengerType.Foods:
                return scanInfo.typeIcons[3];
            case PassengerType.Bottles:
                return scanInfo.typeIcons[4];
            default:
                return scanInfo.typeIcons[0];
        }
    }

    // 清除所有旅客
    public void ClearAllPassenger()
    {
        Debug.Log("清除所有旅客");
        for (int i = 0; i < currentPassenger.Count; i++)
            Destroy(currentPassenger[i]);
        isScan = false;
        currentPassenger.Clear();
    }

    // 显示备忘录
    public void ShowCalepin()
    {
        AudioManager.Instance.PlayButtonSound();
        calepinBtn.DOPlayBackwards();
        calepin.transform.DOLocalMove(calepinPos_0, 0.75f).SetEase(Ease.OutQuad);
        caleMask.SetActive(true);
    }

    // 隐藏备忘录
    public void HideCalepin()
    {
        AudioManager.Instance.PlayButtonSound();
        calepinBtn.DOPlayForward();
        calepin.transform.DOLocalMove(calepinPos_1, 0.75f).SetEase(Ease.OutQuad);
        caleMask.SetActive(false);
        if (!isScan)
        {
            isScan = true;
            NextPassenger();
        }
    }

    // 开始扫描
    private void StartScanPassenger()
    {
        scanTab.transform.position = scanTabPos;
        xrayModel.SetActive(true);
        scanTab.SetActive(true);
    }

    // 停止扫描
    private void StopScanPassenger()
    {
        scanTab.SetActive(false);
        xrayModel.SetActive(false);
    }

    // 玩家选择
    public void PlayerChoice(ButtonType type)
    {
        Debug.Log("玩家选择：" + type);
        StopScanPassenger();
        switch (type)
        {
            case ButtonType.OK:
                if (currentPassenger[0].GetComponent<PassengerAnim>().passengerType != scanType)
                    ChoiceTheRight(false);
                else
                    ChoiceTheError(false);
                break;
            case ButtonType.NOTOK:
                if (currentPassenger[0].GetComponent<PassengerAnim>().passengerType == scanType)
                    ChoiceTheRight(true);
                else
                    ChoiceTheError(true);
                break;
        }
    }

    // 选择正确
    private void ChoiceTheRight(bool fight)
    {
        Debug.Log("选择正确");
        scanRightNum++;
        if (fight)
        {
            // 携带违禁物品
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetBool("Walk", true);
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetTrigger("Reject");
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetTrigger("Scan");
            scanInfo.fightModel.SetTrigger("Dive");
            Invoke("PlayFightEff", 0.5f);
            Invoke("NextPassenger", 2.5f);
            CanvasManager.Instance.ShowChoiceEff(true, "火眼金睛");
        }
        else
        {
            // 未携带违禁物品
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetBool("Walk", true);
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetFloat("SuccessBlend", UnityEngine.Random.Range(0, 0.51f));
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetTrigger("Success");
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetTrigger("Scan");
            Invoke("PassengerExit", 1.75f);
            CanvasManager.Instance.ShowChoiceEff(true, "明察秋毫");
        }

    }

    // 选择错误
    private void ChoiceTheError(bool fight)
    {
        Debug.Log("选择错误");
        if (fight)
        {
            // 未携带违禁物品
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetBool("Walk", true);
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetTrigger("Reject");
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetTrigger("Scan");
            scanInfo.fightModel.SetTrigger("Dive");
            Invoke("PlayFightEff", 0.5f);
            Invoke("NextPassenger", 2.5f);
            CanvasManager.Instance.ShowChoiceEff(false, "不辨真伪");
        }
        else
        {
            // 携带违禁物品
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetBool("Walk", true);
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetFloat("SuccessBlend", UnityEngine.Random.Range(0, 0.51f));
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetTrigger("Success");
            currentPassenger[0].GetComponent<PassengerAnim>().anim.SetTrigger("Scan");
            Invoke("PassengerExit", 1.75f);
            CanvasManager.Instance.ShowChoiceEff(false, "瞒天过海");
        }
    }

    private void PlayFightEff()
    {
        AudioManager.Instance.PlayFightSound();
        scanInfo.fightEff.Play();
    }

    // 旅客离开
    private void PassengerExit()
    {
        currentPassenger[0].transform.DORotate(new Vector3(0, -180f, 0), 0.25f).OnComplete(() =>
        {
            currentPassenger[0].transform.DOMove(passengerExitPos.position, 0.75f).SetEase(Ease.Linear).OnComplete(() =>
            {
                Destroy(currentPassenger[0]);
                currentPassenger.RemoveAt(0);
                NextPassenger();
            });
        });
    }

    // 下一个旅客
    public void NextPassenger()
    {
        if (currentPassenger.Count <= 0)
        {
            CanvasManager.Instance.ShowCompleteMenu();
        }
        else
        {
            for (int i = 0; i < currentPassenger.Count; i++)
            {
                currentPassenger[i].GetComponent<PassengerAnim>().PassengerMove();
            }
            Invoke("StartScanPassenger", 1f);
        }
    }



}

/// <summary>
/// 旅客类型
/// </summary>
public enum PassengerType
{
    // 正常、携带武器、携带工具、携带电子设备、携带食物、携带玻璃瓶
    Normal = 0,
    Weapons = 1,
    Tools = 2,
    Electronic = 3,
    Foods = 4,
    Bottles = 5,
}

/// <summary>
/// 检查类型信息
/// </summary>
[Serializable]
public class ScanTypeInfo
{
    [Header("检查信息")]
    public Text scanTitles;
    public Text dateText;
    public Text timeText;
    public Image typeIcon;
    public Text typeText;
    [Header("类型图片")]
    public List<Sprite> typeIcons;
    [Header("违规处理")]
    public ParticleSystem fightEff;
    public Animator fightModel;
}
